/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package comunicacao;

import comunicacao.ReceiveMessageInterface;
import entities.Teatro;
import static java.awt.SystemColor.text;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import main.ConfigGeral;

/**
 *
 * @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
public class AppDatabaseComunicator {

    private static boolean SHOW_ERROR = false;

    String serverAddress;
    String serverPort;
    Registry registry;
    ReceiveMessageInterface dbServer;

    public AppDatabaseComunicator(String serverAddress, String serverPort) {

        this.serverAddress = serverAddress;
        this.serverPort = serverPort;

        System.out.println("sending " + text + " to " + this.serverAddress + ":" + this.serverPort);

        try {
            registry = LocateRegistry.getRegistry(
                    this.serverAddress,
                    (new Integer(this.serverPort)).intValue()
            );
            dbServer = (ReceiveMessageInterface) (registry.lookup(ConfigGeral.CG_SERVER_DB_NAME));
        } catch (RemoteException e) {
            if (SHOW_ERROR) {
                e.printStackTrace();
            }
        } catch (NotBoundException e) {
            if (SHOW_ERROR) {
                e.printStackTrace();
            }
        }
    }

    public ArrayList<String> listTheaters() {
        try {
            return dbServer.listTheaters();
        } catch (RemoteException e) {
            return null;
        }
    }

    public Teatro getTheater(String theaterName) {
        try {
            return dbServer.getTheater(theaterName);
        } catch (RemoteException e) {
            return null;
        }
    }

    public boolean reserveSeat(String theaterName, int lugarID, int userID, String timestamp) {
        try {
            return dbServer.reserveSeat(theaterName, lugarID, userID, timestamp, false);
        } catch (RemoteException e) {
            return false;
        }
    }

    public boolean purchaseSeat(String theaterName, int lugarID, int userID) {
        try {
            return dbServer.purchaseSeat(theaterName, lugarID, userID, false);
        } catch (RemoteException e) {
            return false;
        }
    }

    public boolean falha() {
        try {
            return dbServer.falha(false, false);
        } catch (RemoteException e) {
            return false;
        }
    }

    public boolean restart() {
        boolean result = false;

        try {
            dbServer = (ReceiveMessageInterface) (registry.lookup("dbServertemp"));
            result = dbServer.restart();
            dbServer = (ReceiveMessageInterface) (registry.lookup("dbServer"));
            return result;
        } catch (RemoteException e) {
            return false;
        } catch (NotBoundException e) {
            return false;
        }
    }

    public boolean removeSeatReserve(String theaterName, int lugarID, int userID) {
        boolean result = false;

        try {
            result = dbServer.removeSeatReserve(theaterName, lugarID, userID, false);
            return result;
        } catch (RemoteException e) {
            return false;
        }
    }
}
